const app = getApp()
import { getShopCartList, deleteOrderCart, updateOrderNum } from '../../../../api/index'

Page({
  data: {
    currentPage: 1,
    pageSize: 15,
    keyword: '',
    list: [],
    sections: []
  },
  search ({ detail }) {
    this.setData({ keyword: detail })
    this.getList()
  },
  cancelAll () {
    this.data.list.forEach(item => {
      item.isChecked = false
      item.materialArray.forEach(row => {
        row.isChecked = false
      })
    })
    this.setData({ list: this.data.list, sections: [] })
  },
  sample () {
    if (!this.data.sections.length) {
      wx.showToast({ title: '请先选择要索样的材料！', icon: 'none' })
      return
    }
    let ids = this.data.sections.map(item => { return item.id }).join(',')
    wx.$nav.navigateTo(`/pages/account/shopCart/confirm/index?form=cart&ids=${ids}`)
  },
  async delMaterial () {
    try {
      if (!this.data.sections.length) {
        wx.showToast({ title: '请先选择要删除的数据！', icon: 'none' })
        return
      }
      const { confirm } = await wx.$pro.showModal({ title: '提示', content: '确定要删除选择的材料吗？' })
      if (confirm) {
        let ids = this.data.sections.map(item => { return item.id }).join(',')
        await deleteOrderCart({ cardIdList: ids })
        wx.showToast({ title: '删除成功！', icon: 'none' })
        this.getList()
      }
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async onChange ({ detail, currentTarget: { dataset: { item, pindex, index } } }) {
    try {
      await updateOrderNum({ cardId: item.id, num: detail, materialId: item.materialId })
      let key = `list[${pindex}].materialArray[${index}].num`
      this.setData({ [key]: detail })
      this.filterSelections()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  selectSupplier ({ currentTarget: { dataset: { item, index } } }) {
    item.isChecked = !item.isChecked
    item.materialArray.forEach(row => {
      row.isChecked = item.isChecked
    })
    let key = `list[${index}]`
    this.setData({ [key]: item })
    this.filterSelections()
  },
  selectMaterial ({ currentTarget: { dataset: { item, index, pindex } } }) {
    item.isChecked = !item.isChecked
    let key = `list[${pindex}].materialArray[${index}]`
    this.setData({ [key]: item })
    this.filterSelections()
  },
  filterSelections () {
    let sections = []
    this.data.list.forEach(item => {
      let rowSections = item.materialArray.filter(row => row.isChecked)
      if (rowSections.length === item.materialArray.length) item.isChecked = true
      else item.isChecked = false
      sections.push(...rowSections)
    })
    this.setData({ list: this.data.list, sections })
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = TextTrackCueList
      let query = {
        currentPage: this.data.currentPage,
        pageLines: this.data.pageSize
      }
      if (this.data.keyword) query.keyWord = this.data.keyword
      const { ResultSet } = await getShopCartList({ ...query })
      this.alloaded = !ResultSet.userData.data.length
      this.data.isEmpty = !ResultSet.userData.data.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.data.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.data.supplierList.push(...ResultSet.userData.data)
        this.setData({
          list: this.data.list,
          currentPage: this.data.currentPage
        })
      }
      this.loading = false
    } catch (err) {
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async getList () {
    try {
      let query = {
        currentPage: 1,
        pageLines: this.data.pageSize
      }
      if (this.data.keyword) query.keyWord = this.data.keyword
      const { ResultSet } = await getShopCartList({ ...query })
      if (ResultSet && ResultSet.userData && ResultSet.userData.cartArray && ResultSet.userData.cartArray.length) {
        ResultSet.userData.cartArray.forEach(item => {
          item.isChecked = false
          item.materialArray.forEach(row => {
            row.isChecked = false
          })
        })
        this.data.isEmpty = ResultSet && ResultSet.userData && ResultSet.userData.cartArray && ResultSet.userData.cartArray.length && ResultSet.userData.cartArray.length < this.data.pageSize
        if (ResultSet.userData.cartArray.length) this.data.currentPage = 2
        this.setData({
          sections: [],
          list: ResultSet.userData.cartArray,
          currentPage: this.data.currentPage,
          isEmpty: this.data.isEmpty
        })
      } else {
        this.setData({
          list: [],
          currentPage: this.data.currentPage,
          isEmpty: true
        })
      }
      
      
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onShow () {
    this.getList()
  }
})